package com.csiec.clock.view.model;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.alibaba.fastjson.JSONObject;
import com.csiec.clock.activity.FaceActivity;
import com.csiec.clock.activity.daka.JoinClockInfoActivity;
import com.csiec.clock.bean.myJoinClock.Information;
import com.csiec.clock.bean.myJoinClock.InformationList;
import com.csiec.clock.bean.myJoinClock.JoinClock;
import com.csiec.clock.bean.people.ResultCode;
import com.csiec.clock.callback.ClockInfoCallBack;
import com.csiec.clock.databinding.ActivityJoinClockInfoBinding;
import com.csiec.clock.model.ClockModel;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.HashMap;
import java.util.Map;

import static com.qmuiteam.qmui.widget.dialog.QMUITipDialog.Builder.ICON_TYPE_FAIL;

public class JoinClockInfoViewModel extends ViewModel {

    private final String TAG = JoinClockInfoActivity.class.getSimpleName();
    @SuppressLint("StaticFieldLeak")
    private Activity activity;
    private ActivityJoinClockInfoBinding binding;
    private String checkFace;
    private JoinClock ownClock;
    private InformationList informationList;
    private MutableLiveData<String> studentNumber;
    private MutableLiveData<String> faceInfo;
    private MutableLiveData<String> position;
    private MutableLiveData<String> latitude;       //经度
    private MutableLiveData<String> longitude;      //纬度
    private MutableLiveData<String> studentName;    //姓名
    private MutableLiveData<String> temperature;    //体温
    private MutableLiveData<String> phone;          //电话
    private MutableLiveData<String> classes;        //班级
    private MutableLiveData<String> remarks;        //备注
    private MutableLiveData<String> major;          //专业
    private final Handler handler = new Handler();
    final String[] notRequired = new String[]{"姓名", "体温", "手机号", "班级", "备注", "专业"};
    final String[] required = new String[]{"*姓名", "*体温", "*手机号", "*班级", "*备注", "*专业"};
    final String[] resName = new String[]{"studentName", "temperature", "phone", "classes", "remarks", "major"};
    private TextView[] textViews;
    private MutableLiveData[] mutableLiveDataList;

    private ClockModel model;


    public void init(Activity activity, ActivityJoinClockInfoBinding binding, String checkFace, JoinClock ownClock) {
        this.activity = activity;
        this.binding = binding;
        this.checkFace = checkFace;
        this.ownClock = ownClock;
        model = new ClockModel();
        textViews = new TextView[]{binding.textView18, binding.textView19, binding.textView20, binding.textView21, binding.textView22, binding.textView23};
        informationList = JSONObject.parseObject(ownClock.getInformationRequired(), InformationList.class);
        loadRequired();
        if (studentNumber == null) {
            studentNumber = new MutableLiveData<>();
        }
        if (faceInfo == null) {
            faceInfo = new MutableLiveData<>();
            faceInfo.setValue(checkFace);
        }
        if (position == null) {
            position = new MutableLiveData<>();
        }

        if (studentName == null) {
            studentName = new MutableLiveData<>();
        }
        if (temperature == null) {
            temperature = new MutableLiveData<>();
        }
        if (phone == null) {
            phone = new MutableLiveData<>();
        }
        if (classes == null) {
            classes = new MutableLiveData<>();
        }
        if (remarks == null) {
            remarks = new MutableLiveData<>();
        }
        if (major == null) {
            major = new MutableLiveData<>();
        }
        if (latitude == null) {
            latitude = new MutableLiveData<>();
        }
        if (longitude == null) {
            longitude = new MutableLiveData<>();
        }

    }


    //加载必填项目
    private void loadRequired() {
        for (Information information : informationList.getInformationList()) {
            textViews[information.getId() - 1].setText(required[information.getId() - 1]);
        }
    }

    //
    @SuppressLint("ResourceAsColor")
    public void getFace() {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("启动界面中...")
                .create();
        tipDialog.show();
        Intent intent = new Intent(activity, FaceActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("activityInfo", "1");
        intent.putExtra("join_card", ownClock);
        activity.startActivity(intent);
        binding.textView25.postDelayed(new Runnable() {
            @Override
            public void run() {
                tipDialog.dismiss();
                activity.finish();
            }
        }, 2000);
    }

    //打卡
    public void goClock() {
        Map<String, String> res = new HashMap<>();
        mutableLiveDataList = new MutableLiveData[]{studentName, temperature, phone, classes, remarks, major};
        //活体检测
        if (!"验证成功".equals(checkFace)) {
            showTip(ICON_TYPE_FAIL, "人脸验证失败！");
            return;
        }
        //是否获取到定位
        if (position.getValue() == null) {
            showTip(ICON_TYPE_FAIL, "请点击获取位置信息");
            return;
        }
        //必填项目信息
        for (Information information : informationList.getInformationList()) {

            if (mutableLiveDataList[information.getId() - 1].getValue() == null) {
                showTip(ICON_TYPE_FAIL, "请填写 " + notRequired[information.getId() - 1] + " 信息");
                return;
            } else {
                res.put(resName[information.getId() - 1], String.valueOf(mutableLiveDataList[information.getId() - 1].getValue()));
            }
        }

        showTip(QMUITipDialog.Builder.ICON_TYPE_LOADING, "打卡中!!!");
        //存储信息

        model.goClock(ownClock.getStaffId(), res.toString(), latitude.getValue(), longitude.getValue(), new ClockInfoCallBack() {
            @Override
            public void onSuccess(ResultCode backData) {
                if (backData.getStatus() == 200) {
                    handler.post(() -> {
                        showTip(QMUITipDialog.Builder.ICON_TYPE_SUCCESS, "打卡成功");
                    });

                } else {
                    handler.post(() -> {
                        showTip(ICON_TYPE_FAIL, backData.getMessage());
                    });
                }
            }

            @Override
            public void onFailed() {
                handler.post(() -> {
                    showTip(QMUITipDialog.Builder.ICON_TYPE_FAIL, "网络异常");
                });
            }
        });


    }


    //提示悬浮窗
    public void showTip(int type, String msg) {
        QMUITipDialog tipDialog = new QMUITipDialog.Builder(activity)
                .setIconType(type)
                .setTipWord(msg)
                .create();
        tipDialog.show();
        binding.textView12.postDelayed(tipDialog::dismiss, 1000);
    }

    public MutableLiveData<String> getStudentNumber() {
        return studentNumber;
    }

    public MutableLiveData<String> getPosition() {
        return position;
    }

    public void setPosition(String address) {
        position.setValue(address);
    }

    public MutableLiveData<String> getFaceInfo() {
        return faceInfo;
    }

    public MutableLiveData<String> getStudentName() {
        return studentName;
    }

    public MutableLiveData<String> getTemperature() {
        return temperature;
    }

    public MutableLiveData<String> getPhone() {
        return phone;
    }

    public MutableLiveData<String> getClasses() {
        return classes;
    }

    public MutableLiveData<String> getRemarks() {
        return remarks;
    }

    public MutableLiveData<String> getMajor() {
        return major;
    }

    public MutableLiveData<String> getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude.setValue(latitude);
    }

    public MutableLiveData<String> getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude.setValue(longitude);
    }
}
